package com.cgw.game.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * @Description TODO
 * @Author YF
 * @Date 2020/8/26
 * @Version 1.0
 */
public class DiscountMarkerView extends View {
    /**
     * 背景画笔
     * */
    private Paint mBgPaint;
    /**
     * 文字画笔
     * */
    private Paint mTextPaint;
    /**
     * 宽
     * */
    private int width;
    /**
     * 高
     * */
    private int height;


    /**
     * 上下文对象
     * */
    private Context context;


    private String text = "折扣";

    public DiscountMarkerView(Context context) {
        this(context,null);
    }

    public DiscountMarkerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public DiscountMarkerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initView();
    }

    private void initView() {

        mBgPaint = new Paint();
        mBgPaint.setAntiAlias(true);
        mBgPaint.setStyle(Paint.Style.FILL);
        mBgPaint.setColor(Color.RED);

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTextSize(45f);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        height = measureSize(widthMeasureSpec);
        width = measureSize(heightMeasureSpec);
        if(width<height){
            height = width;
        }else {
            width = height;
        }
        setMeasuredDimension(width,height);
    }
    private int measureSize(int measureSpec) {
        int length;
        int mode = MeasureSpec.getMode(measureSpec);
        int size = MeasureSpec.getSize(measureSpec);
        if(mode == MeasureSpec.EXACTLY){
            length = size;
        }else{
            length = DensityUtil.dip2px(context,60);
            if(mode == MeasureSpec.AT_MOST){
                length = Math.min(length,size);
            }
        }
        return length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawBackGround(canvas);
    }

    private void drawBackGround(Canvas canvas) {
        canvas.translate(0,height);
        canvas.save();
        canvas.rotate(-45f);
        float bgWidth = (float) Math.sqrt(width*width+height*height);
        float bgHeight = measureTextHeight()+10;
        canvas.drawRect(0,-bgHeight,bgWidth,0,mBgPaint);

        float textWidth = mTextPaint.measureText("送2元");
        canvas.drawText("送2元",bgWidth/2-textWidth/2,-15,mTextPaint);

        canvas.restore();
        canvas.save();
    }


    public float measureTextHeight(){
        float height = 0f;
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        height = fontMetrics.descent - fontMetrics.ascent;
        return height;
    }
}
